package tool;


import model.Flower;
import model.Orderitem;

import java.util.HashMap;
import java.util.Map;


public class Cart {
    private Map items;

    public Cart() {
        if (items == null)
            items = new HashMap<Integer, Orderitem>();
    }

    public Map getItems() {
        return items;
    }

    public void setItems(Map items) {
        this.items = items;
    }

    public void addFlower(int flowerId, Orderitem orderitem) {
        if (items.containsKey(flowerId)) {
            Orderitem _orderitem = (Orderitem) items.get(flowerId);
            orderitem.setQuantity(_orderitem.getQuantity() + orderitem.getQuantity());
            items.put(flowerId, orderitem);
        } else
            items.put(flowerId, orderitem);
    }

    public void updateCart(int flowerId, int quantity) {
        Orderitem orderitem = (Orderitem) items.get(flowerId);
        orderitem.setQuantity(quantity);
        items.put(flowerId, orderitem);
    }

    public int getTotalPrice() {
        int totalPrice = 0;
        for (Object o : items.values()) {
            Orderitem orderitem = (Orderitem) o;
            Flower flower = orderitem.getFlower();
            int quantity = orderitem.getQuantity();
            totalPrice += flower.getPrice() * quantity;
        }
        return totalPrice;
    }
}
